class Solution {
public:
    bool dfs(TreeNode* root, int targetSum, int sum)
    {
        if (root == nullptr) return false;
        if (root->left == nullptr && root->right == nullptr)
        {
            return sum + root->val == targetSum;
        }
        return dfs(root->left, targetSum, sum + root->val) || dfs(root->right, targetSum, sum + root->val);
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (root == nullptr) return false;
        return dfs(root, targetSum, 0);
    }
};